package com.aistar.service.impl;

import com.aistar.mapper.AttentionMapper;
import com.aistar.pojo.Attention;
import com.aistar.pojo.AttentionExample;
import com.aistar.service.AttentionService;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AttentionServiceImpl implements AttentionService {

    @Autowired
    private AttentionMapper attentionMapper;
    @Override
    public ServerResponse save(Attention attention) {
        int rows = attentionMapper.insert(attention);
        if (rows >0 ){
            return ServerResponse.addSuccess();
        }else {
            return ServerResponse.addFailed();
        }
    }


    //删除商品 将 状态修改
    @Override
    public ServerResponse modifyStatus(Attention attention) {
        attention.setAttentionStatus(0);
        int rows = attentionMapper.updateByPrimaryKeySelective(attention);
        if (rows>0){
            return ServerResponse.deleteSuccess();
        }else {
            return ServerResponse.deleteFailed();
        }

    }

    @Override
    public ServerResponse modify(Attention attention) {
        return null;
    }

    @Override
    public ServerResponse getById(Integer id) {
        Attention attention = attentionMapper.selectByPrimaryKey(id);
        if (attention !=null){
            return ServerResponse.getDataSuccess(attention);
        }else {
            return ServerResponse.getDataFailed("查询失败");
        }
    }

    @Override
    public ServerResponse getAll() {
        return null;
    }

    //根据用户id 商品id 查询出对应的关注商品
    @Override
    public ServerResponse getByCustomerIdProductId(Integer customerId, Integer productId) {
        AttentionExample attentionExample = new AttentionExample();
        attentionExample.createCriteria().andCustomerIdEqualTo(customerId)
                                         .andProductIdEqualTo(productId)
                                            .andAttentionStatusEqualTo(1);
        List<Attention> attentionList = attentionMapper.selectByExample(attentionExample);
        if (attentionList != null && attentionList.size()>0){
            return ServerResponse.getDataSuccess(attentionList);
        }else {
            return ServerResponse.getDataFailed("用户没有关注该商品");
        }

    }


    //根据用户id  查询用户收藏的所有商品
    @Override
    public ServerResponse getAllByCustomerId(Integer customerId) {
        AttentionExample attentionExample = new AttentionExample();
        attentionExample.createCriteria().andAttentionStatusEqualTo(1)
                                            .andCustomerIdEqualTo(customerId);
        List<Attention> attentionList = attentionMapper.selectByExample(attentionExample);
        if (attentionList != null && attentionList.size()>0){
            return ServerResponse.getDataSuccess(attentionList);
        }else {
            return ServerResponse.getDataFailed("您未关注过任何商品");
        }

    }
}
